package com.taiji.dianthus.service.http.impl;

import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.sys.UserRepository;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.http.HttpAPIService;
import com.taiji.dianthus.service.http.SynUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysUserInfoServiceImpl
 * @Description 用户同步服务实现类
 * @Author H.M
 * @Date 2020/5/27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserInfoServiceImpl implements SynUserInfoService {


    @Autowired
    private HttpAPIService httpAPIService;

    @Autowired
    private UserRepository userRepository;

    /**
     * 同步用户信息
     *
     * @param url
     * @return
     */
    @Override
    public BusinessMessage SynAllUser(String url) {
        //获取系统所有 业务用户 信息
        List<Map<String, Object>> userList = userRepository.listAllBusinessUser();
        JSONArray userJsonArray = new JSONArray(new ArrayList<>(userList));
        return  httpAPIService.doPost(url,userJsonArray);
    }

    /**
     * 删除单个用户
     *
     * @param url
     * @param userId
     */
    @Override
    public BusinessMessage removeUser(String url, String userId) {
        Map userIdMap = new HashMap(16);
        userIdMap.put("id",userId);
        return httpAPIService.doGet(url,userIdMap);
    }

    /**
     * 更新或新增 单个用户信息
     *
     * @param url
     * @param userId
     */
    @Override
    public BusinessMessage updateUser(String url, String userId) {
        BusinessMessage message = new BusinessMessage();
        Map userMap = new HashMap(16);
        DSysUser dSysUser =  userRepository.getById(userId);
        if (null != dSysUser ) {
            userMap.put("id",dSysUser.getId());
            userMap.put("userName",dSysUser.getName());
            userMap.put("userLoginName",dSysUser.getUserLoginName());
            userMap.put("orgId",dSysUser.getOrgId());
            userMap.put("userDuty",dSysUser.getUserDuty());
            userMap.put("userPhone",dSysUser.getUserPhone());
            userMap.put("userOrder",dSysUser.getUserOrder());
            userMap.put("userMail",dSysUser.getUserMail());
            userMap.put("userPosition",dSysUser.getUserPosition());
            userMap.put("userSex",dSysUser.getUserSex());
            userMap.put("officePhone",dSysUser.getOfficePhone());
            httpAPIService.doPost(url, userMap);
        }else {
            message.setMsg("用户不存在");
        }

        return message;
    }
}
